package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.DiaryBean;
import com.ykdl.member.kid.beans.DiaryListBeanDay;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.AngleDiaryActivity;
import com.ykdl.member.kid.widget.AsyncImageView;

public class DiaryAdapter extends BaseAdapter {
	
	private Context context;
	private List<DiaryListBeanDay> list = new ArrayList<DiaryListBeanDay>();
	private String activity = AngleDiaryActivity.TAG;
	private BitmapUtils mBmpUtils;
	
	public DiaryAdapter(Context c){
		this.context = c;
		mBmpUtils = new BitmapUtils(c);
	}
	
	public void addBeans(List<DiaryListBeanDay> beans){
		if (beans != null) {
			for (DiaryListBeanDay beanday : beans) {
				this.list.add(beanday);
			}
		}
	}
	
	public void clean(){
		list.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderView holder = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_diary, null);
			holder = new HolderView();
			holder.txtDate = (TextView) convertView.findViewById(R.id.txtDate);
			holder.img0 = (ImageView) convertView.findViewById(R.id.asynImg0);
			holder.layerImage = convertView.findViewById(R.id.layerImage);
			holder.img1 = (ImageView) convertView.findViewById(R.id.asynImg1);
			holder.img2 = (ImageView) convertView.findViewById(R.id.asynImg2);
			holder.img3 = (ImageView) convertView.findViewById(R.id.asynImg3);
			holder.img4 = (ImageView) convertView.findViewById(R.id.asynImg4);
			holder.img5 = (ImageView) convertView.findViewById(R.id.asynImg5);
			holder.img6 = (ImageView) convertView.findViewById(R.id.asynImg6);
			holder.layerMore = convertView.findViewById(R.id.layerMore);
			holder.txtCount = (TextView) convertView.findViewById(R.id.txtCount);
			convertView.setTag(holder);
		} else {
			holder = (HolderView) convertView.getTag();
		}
		DiaryListBeanDay bean = (DiaryListBeanDay) getItem(position);
		holder.txtDate.setText(bean.getDate());
		
		if (bean.getList() != null) {
			int size = bean.getList().size();
			for (int i = 0; i < size; i++) {
				DiaryBean b = bean.getList().get(i);
				if (size > 1) {
					if (i == 0) {
						mBmpUtils.display(holder.img1, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
					} else if (i == 1) {
						mBmpUtils.display(holder.img2, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
					} else if (i == 2) {
						mBmpUtils.display(holder.img3, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
					} else if (i == 3) {
						mBmpUtils.display(holder.img4, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
					} else if (i == 4) {
						mBmpUtils.display(holder.img5, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
					} else if (i == 5) {
						mBmpUtils.display(holder.img6, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
					} else if (i >= 6) {
						break;
					}
					holder.img0.setVisibility(View.GONE);
					holder.layerImage.setVisibility(View.VISIBLE);
				} else {
					holder.img0.setVisibility(View.VISIBLE);
					holder.layerImage.setVisibility(View.GONE);
					mBmpUtils.display(holder.img0, b.getFile_metas().get(0).getDownload_urls().getMiddle().getUrl());
				}
			}
			
			if (size == 1) {
				holder.img0.setVisibility(View.VISIBLE);
				holder.layerImage.setVisibility(View.GONE);
			}
			if (size > 6) {
				holder.layerMore.setVisibility(View.VISIBLE);
				holder.txtCount.setText("共"+size+"张");
			} else {
				holder.layerMore.setVisibility(View.GONE);
			}
			if (size == 0) {
				holder.layerImage.setVisibility(View.GONE);
			} else if (size == 1) {
				holder.img1.setVisibility(View.VISIBLE);
				holder.img2.setVisibility(View.GONE);
				holder.img3.setVisibility(View.GONE);
				holder.img4.setVisibility(View.GONE);
				holder.img5.setVisibility(View.GONE);
				holder.img6.setVisibility(View.GONE);
			} else if (size == 2) {
				holder.img1.setVisibility(View.VISIBLE);
				holder.img2.setVisibility(View.VISIBLE);
				holder.img3.setVisibility(View.GONE);
				holder.img4.setVisibility(View.GONE);
				holder.img5.setVisibility(View.GONE);
				holder.img6.setVisibility(View.GONE);
			} else if (size == 3) {
				holder.img1.setVisibility(View.VISIBLE);
				holder.img2.setVisibility(View.VISIBLE);
				holder.img3.setVisibility(View.VISIBLE);
				holder.img4.setVisibility(View.GONE);
				holder.img5.setVisibility(View.GONE);
				holder.img6.setVisibility(View.GONE);
			} else if (size == 4) {
				holder.img1.setVisibility(View.VISIBLE);
				holder.img2.setVisibility(View.VISIBLE);
				holder.img3.setVisibility(View.VISIBLE);
				holder.img4.setVisibility(View.VISIBLE);
				holder.img5.setVisibility(View.GONE);
				holder.img6.setVisibility(View.GONE);
			} else if (size == 5) {
				holder.img1.setVisibility(View.VISIBLE);
				holder.img2.setVisibility(View.VISIBLE);
				holder.img3.setVisibility(View.VISIBLE);
				holder.img4.setVisibility(View.VISIBLE);
				holder.img5.setVisibility(View.VISIBLE);
				holder.img6.setVisibility(View.GONE);
			} else {
				holder.img1.setVisibility(View.VISIBLE);
				holder.img2.setVisibility(View.VISIBLE);
				holder.img3.setVisibility(View.VISIBLE);
				holder.img4.setVisibility(View.VISIBLE);
				holder.img5.setVisibility(View.VISIBLE);
				holder.img6.setVisibility(View.VISIBLE);
			}
		}
		return convertView;
	}

	private class HolderView{
		TextView txtDate;
		View layerImage;
		ImageView img0;
		ImageView img1,img2,img3,img4,img5,img6;
		View layerMore;
		TextView txtCount;
	}
}
